﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Account_ModularModify : BasePage
{
    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        base.CheckUserPermission("001001");

        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");
            if (Action == "Update")
            {
                getDate(Id);
            }
        }
    }
    #endregion

    #region 加载数据
    private void getDate(int id)
    {
        sys_modular model = new sys_modular();
        sys_modularBLL bll = new sys_modularBLL();
        model = bll.GetModel(id);
        this.txt_ModularName.Text = model.ModularName;
        this.txt_ModularCode.Text = model.ModularCode;
        this.txt_OrderNo.Text = model.OrderNo.ToString();
        this.radio_Valid.SelectedValue = model.Valid.ToString();
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        if (Action == "Update")
        {
            #region 修改
            try
            {
                sys_modular model = new sys_modular();
                sys_modularBLL bll = new sys_modularBLL();

                //检验数据是否重复
                string strCheck = string.Format("(ModularName like '{0}' or ModularCode like '{1}') and ModularId<>{2}",
                    TextUtility.ReplaceText(this.txt_ModularName.Text.Trim()),
                    TextUtility.ReplaceText(this.txt_ModularCode.Text.Trim()),
                    Id.ToString());
                if (bll.GetModelList(strCheck).Count > 0)
                {
                    base.Alert("模块名称或模块编码已存在！");
                    return;
                }

                model = bll.GetModel(Id);
                model.ModularName = this.txt_ModularName.Text.Trim();
                model.ModularCode = this.txt_ModularCode.Text.Trim();
                model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                model.UpdateUser = base.CurrentSession.UserID;
                model.UpdateDate = DateTime.Now;
                bll.Update(model);

                base.Redirect("操作成功！", "ModularList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }
        else
        {
            #region 新增
            try
            {
                sys_modular model = new sys_modular();
                sys_modularBLL bll = new sys_modularBLL();

                //检验数据是否重复
                string strCheck = string.Format("ModularName like '{0}' or ModularCode like '{1}'",
                    TextUtility.ReplaceText(this.txt_ModularName.Text.Trim()),
                    TextUtility.ReplaceText(this.txt_ModularCode.Text.Trim()));
                if (bll.GetModelList(strCheck).Count > 0)
                {
                    base.Alert("模块名称或模块编码已存在！");
                    return;
                }

                model.ModularName = this.txt_ModularName.Text.Trim();
                model.ModularCode = this.txt_ModularCode.Text.Trim();
                model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                model.CreateUser = base.CurrentSession.UserID;
                model.CreateDate = DateTime.Now;
                model.UpdateUser = model.CreateUser;
                model.UpdateDate = model.CreateDate;
                bll.Add(model);

                base.Redirect("操作成功！", "ModularList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }

    }
    #endregion
}
